package com.imooc.springcloud.biz;

import com.imooc.springcloud.topic.DelayedTopic;
import com.imooc.springcloud.topic.DlqTopic;
import com.imooc.springcloud.topic.ErrorTopic;
import com.imooc.springcloud.topic.FallbackTopic;
import com.imooc.springcloud.topic.GroupTopic;
import com.imooc.springcloud.topic.MyTopic;
import com.imooc.springcloud.topic.RequeueTopic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author L
 */
@RestController
@Slf4j
public class Controller {

    @Autowired
    private MyTopic producer;

    @Autowired
    private GroupTopic groupTopicProducer;

    @Autowired
    private DelayedTopic delayedTopicProducer;

    @Autowired
    private ErrorTopic errorTopicProducer;

    @Autowired
    private RequeueTopic requeueTopicProducer;

    @Autowired
    private DlqTopic dlqTopicProducer;

    @Autowired
    private FallbackTopic fallbackTopicProducer;

    @GetMapping("send")
    public void sendMessage(@RequestParam("body") String body) {
        producer.output().send(MessageBuilder.withPayload(body).build());
    }

    @GetMapping("sendToGroup")
    public void sendMessageToGroup(@RequestParam("body") String body) {
        groupTopicProducer.output().send(MessageBuilder.withPayload(body).build());
    }

    @GetMapping("sendDM")
    public void sendDelayedMessage(@RequestParam("body") String body, @RequestParam("seconds") Integer seconds) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);
        log.info("ready to send delayed message");
        delayedTopicProducer.output().send(MessageBuilder.withPayload(msg)
                .setHeader("x-delay", 1000 * seconds)
                .build());
    }

    // 异常重试(单机版)
    @GetMapping("sendError")
    public void sendErrorMessage(@RequestParam("body") String body) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);
        errorTopicProducer.output().send(MessageBuilder.withPayload(msg).build());
    }

    // 异常重试(联机版-重新入队)
    @GetMapping("requeue")
    public void sendErrorMessageToMQ(@RequestParam("body") String body) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);
        requeueTopicProducer.output().send(MessageBuilder.withPayload(msg).build());
    }

    // 死信队列
    @GetMapping("dlq")
    public void sendErrorMessageToDlq(@RequestParam("body") String body) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);
        dlqTopicProducer.output().send(MessageBuilder.withPayload(msg).build());
    }

    // fallback
    @GetMapping("fallback")
    public void sendErrorMessageToFallback(@RequestParam("body") String body, @RequestParam("version") String version) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);

        fallbackTopicProducer.output().send(MessageBuilder
                .withPayload(msg)
                .setHeader("version", version)
                .build());
    }
}
